
const Delivery = require('../db/models/delivery');
const Shelve = require('../db/models/shelve');
module.exports = {

    GetShelves(req, res) {
        Shelve.findAll()
            .then(data => {
                res.json({
                    code: 200,
                    message: '获取成功',
                    data: data
                });
            })
            .catch(err => {
                res.status(500).json({
                    code: 500,
                    message: '获取失败',
                    error: err
                });
            });
    },
    GetShelve(req, res) {
        Shelve.findById(req.params.id)
            .then(data => {
                if (data == null) {
                    return res.status(404).json({
                        code: 404,
                        message: '未找到货架'
                    });
                }
                res.json({
                    code: 200,
                    message: '获取成功',
                    data: data
                });
            })
            .catch(err => {
                res.status(500).json({
                    code: 500,
                    message: '获取失败',
                    error: err
                });
            });
    },

    AddShelves(req, res) {
        const { name } = req.body
        Shelve.create({
            name: name
        }).then(data => {
            res.json({
                code: 200,
                message: '添加成功',
                data: data
            })
        })
    },

    UpdateShelves(req, res) {
        const id = req.params.id;
        const { name } = req.body;
        Shelve.update({ name: name }, {
            where: { id: id }
        })
            .then(data => {
                res.json({
                    code: 200,
                    message: '更新成功',
                    data: data
                });
            })
            .catch(err => {
                res.status(500).json({
                    code: 500,
                    message: '更新失败',
                    error: err
                });
            });
    },

    DeleteShelves(req, res) {
        const id = req.params.id;
        Shelve.destroy({
            where: { id: id }
        })
            .then(data => {
                res.json({
                    code: 200,
                    message: '删除成功',
                    data: data
                });
            })
            .catch(err => {
                res.status(500).json({
                    code: 500,
                    message: '删除失败',
                    error: err
                });
            });
    },

    // 查询货架下面的所有空快递
    GetShelveDeliveries(req, res) {
        // 货架id
        const id = req.params.id;
        Delivery.findAll({
            where: {
                shelve_id: id,

            }

        }).then(data => {
            res.json({
                code: 200,
                message: '获取成功',
                data: data
            });
        })
    }
}